#!/usr/bin/env bash
set -euo pipefail

dn=$(dirname "$0")
# shellcheck source=src/cmdlib.sh
. "${dn}"/cmdlib.sh

print_help() {
    cat 1>&2 <<'EOF'
Usage: coreos-assembler clean --help
       coreos-assembler clean

  Delete all build artifacts.
EOF
}

rc=0
options=$(getopt --options h --longoptions help -- "$@") || rc=$?
[ $rc -eq 0 ] || {
    print_help
    exit 1
}
eval set -- "$options"
while true; do
    case "$1" in
        -h | --help)
            print_help
            exit 0
            ;;
        --)
            shift
            break
            ;;
        *)
            fatal "$0: unrecognized option: $1"
            exit 1
            ;;
    esac
    shift
done

if [ $# -ne 0 ]; then
    print_help
    fatal "ERROR: Too many arguments"
    exit 1
fi

set -x
# This has some useful sanity checks
prepare_build

# But go back to the toplevel
cd "${workdir:?}"
# Note we don't prune the cache.  If you want that, just rm -rf them.
rm -rf builds/* tmp/*
